﻿package {

	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.display.Sprite;
	import com.greensock.*;
	import flash.ui.Mouse;

	public class MenuItemHandler extends Sprite {
		
		private var parentSprite:Object3D;
		private var menuItems:Vector.<MenuItem>;
		
		private var spriteBoard:SpriteBoard;
		
		private var descriptionArray:Vector.<MenuText>;
		private var scaledAlpha:Number;
		
		private static const MAX_VERTEX_SIZE = 20;
		private static const MIN_VERTEX_SIZE = 10;
		
		
		
		private var init:Boolean;
		
		public function MenuItemHandler(parentSprite:Object3D,
										spriteBoard:SpriteBoard)
		{
			scaledAlpha = 0;
			
			this.parentSprite = parentSprite;
			this.spriteBoard = spriteBoard;
			
			menuItems = new Vector.<MenuItem>;
			descriptionArray = new Vector.<MenuText>;
		}
		
		public function addNewItem(position:Point, description:String, url:String) {
			
			init = false;
			
			var newMenuText:MenuText = new MenuText();
			var newMenuItem:MenuItem = new MenuItem();	

			
			newMenuItem.x = position.x; 				// set the default position,
			newMenuItem.y = position.y;					// description and URL
			
			newMenuText.x = position.x; 				// set the default position,
			newMenuText.y = position.y;					// description and URL
			
			newMenuItem.linkTo = url;
			newMenuItem.descriptionRef = newMenuText;
			newMenuText.menuText.text = description;
			
			
			newMenuItem.mouseChildren = false; 
			newMenuItem.addEventListener(MouseEvent.CLICK, itemClicked);
			newMenuItem.addEventListener(MouseEvent.MOUSE_OVER, itemOver);
			newMenuItem.addEventListener(MouseEvent.MOUSE_OUT, itemOut);
			
			menuItems.push(newMenuItem);
			descriptionArray.push(newMenuText);
			
			spriteBoard.addChild(newMenuItem);
			spriteBoard.addChild(newMenuText);
			
			init = true;
		}
		
		public function itemClicked(e:Event):void {
			var urlRequest:URLRequest = new URLRequest(e.target.linkTo);
			navigateToURL(urlRequest, "_self");
		}
		
		public function itemOut(e:Event):void {
			trace("over");
			Mouse.cursor = "arrow";
			TweenMax.to(e.target, 1, {alpha: scaledAlpha, glowFilter:{color:0xffffff, alpha:1, blurX:0, blurY:0},  blurFilter:{blurX:0, blurY:0}});
			TweenLite.to(e.target.descriptionRef, 0.5, {scaleX:1, scaleY:1});
			
			spriteBoard.startRotation();
		}
		
		public function itemOver(e:Event):void {
			// change pointer; stop shape rotation; make larger; tween
			Mouse.cursor = "button";
			TweenMax.to(e.target, 0.1, {alpha: 1, glowFilter:{color:0xffffff, alpha:1, blurX:30, blurY:30},  blurFilter:{blurX:1, blurY:1}});
			TweenLite.to(e.target.descriptionRef, 0.5, {scaleX:1.2, scaleY:1.2});
			
			//TweenMax.to(textDisplay.menuText, 0.1, {alpha: 1, glowFilter:{color:0xffffff, alpha:1, blurX:60, blurY:60},  blurFilter:{blurX:1, blurY:1}});
			//textDisplay.menuText.text = e.target.urlDescription;
			
			spriteBoard.haltRotation();
		}
		
		public function setPosition(index:int, vertex:Point, pointScale:Number) {
			if(init == true) {
				
				scaledAlpha = (0.4 * (1 - pointScale)) + 0.6;
				var textAlpha:Number = (0.9 * (1 - pointScale));
				
				menuItems[index].alpha = scaledAlpha;
				descriptionArray[index].alpha = textAlpha;
				
				var size:Number = ((MAX_VERTEX_SIZE - MIN_VERTEX_SIZE) * (1 - pointScale));
				size = MIN_VERTEX_SIZE + size;
				
				menuItems[index].width = size;
				menuItems[index].height = size;
				
				descriptionArray[index].x = vertex.x;
				descriptionArray[index].y = vertex.y + 20;
				
				menuItems[index].x = vertex.x;
				menuItems[index].y = vertex.y;
			}
			
		}
		
		public function showText():void {
			for(var i:int = 0; i < descriptionArray.length; i++) {
				//TweenMax.to(descriptionArray[i], 0.5, {alpha: 1, glowFilter:{color:0xffffff, alpha:1, blurX:0, blurY:0},  blurFilter:{blurX:0, blurY:0}});
			}
		}
		
		public function hideText():void {
			for(var i:int = 0; i < descriptionArray.length; i++) {
				//TweenMax.to(descriptionArray[i], 0.5, {alpha: 0, glowFilter:{color:0xffffff, alpha:0, blurX:0, blurY:0},  blurFilter:{blurX:0, blurY:0}});
			}
		}
	}

}